require.config({
	paths: {
		'jquery' : 'jquery-1.8.3.min'
	}
});

require(['jquery','cookie'], function ($,cookie) {
	var aGoods = cookie.getCookie('shopCar');
	if(aGoods){
		aGoods = JSON.parse(cookie.getCookie('shopCar'));
		var sHtml = '';
		aGoods.forEach(function (v) {
			sHtml += `
			<li>
				<a href="javascript:;" class="del" data-id="${v.id}">删除该商品</a>
				<img src="${v.url}" alt="">
				<div class="goods-name">${v.name}</div>
				<div class="goods-price">${v.price}元，共${v.num}件</div>
			</li>
			`
		});
		$('#list').html(sHtml);

		//删除该商品
		$('.del').click(function () {
			var iId = $(this).data('id');
			for(var i = 0;i < aGoods.length; i++){
				if(aGoods[i].id == iId){
					//从cookie中删除
					aGoods.splice(i,1);
					//更新cookie
					cookie.addCookie('shopCar', JSON.stringify(aGoods), 7);
					//从DOM树中删除
					$(this).parent().remove();
					break;
				}
			}
		});
	}
});